//本js主要聚合ipcMain.on事件
const logger = require('electron-log')
const {ipcMain, BrowserWindow, powerMonitor, dialog, app} = require('electron')

const Store = require('electron-store')
const {getOpenFilePath,readFile,writeFile} = require("./fileRead");




const store = new Store()
//获取当前窗口


//开启监听
exports.ipcMainOn = function () {

    mainWindowAction()

    electronStoreInit()

    loggerInit()

    fileReadInit()
}


//监听主窗口显示
function mainWindowAction() {

    myWindows.mainWindow.on('maximize', () => {
        myWindows.mainWindow.webContents.send('maxWindows', true)
    })
    myWindows.mainWindow.on('unmaximize', () => {
        myWindows.mainWindow.webContents.send('maxWindows', false)
    })

    //监听显示主窗口
    ipcMain.on('window-show', (event, arg) => {
        myWindows.mainWindow.show()
    })

    //监听窗口最小化
    ipcMain.on('window-min', (event, arg) => {
        myWindows.mainWindow.minimize()
    })

    //监听窗口最大化
    ipcMain.on('window-max', (event, arg) => {
        myWindows.mainWindow.maximize()
    })

    //监听窗口还原
    ipcMain.on('window-restore', (event, arg) => {
        myWindows.mainWindow.restore()
        myWindows.mainWindow.center();
        if (myWindows.mainWindow.isMaximized()) {
            myWindows.mainWindow.unmaximize()
        }
    })

    //监听主页面关闭按钮使用退出程序
    ipcMain.on('window-quit', (event, arg) => {
        // myWindows.mainWindow.close()
        logger.info('window-quit')
        app.quit()
    })

    //监听关闭窗口到托盘
    ipcMain.on('window-close-to-tray', (event, arg) => {
        myWindows.mainWindow.hide()
    })

    //mainWindowsShow
    ipcMain.on('mainWindowsShow', (event, arg) => {
        myWindows.mainWindow.show()
    })


    //getSystemInfo
    ipcMain.on('getSystemInfo', (event, arg) => {
        event.returnValue = {
            systemType: myWindows.systemType,
            systemVersion: myWindows.systemVersion,
        }
    })

}


//electronStore初始化
function electronStoreInit() {
    ipcMain.on("electronStoreGet", (ipcEvent, key) => {
        ipcEvent.returnValue = store.get(key, null);
    });

    ipcMain.on("electronStoreSet", (ipcEvent, arg) => {
        try {
            store.set(arg.name, arg.val)
        } catch (e) {
            logger.error(e)
        }
    });

    ipcMain.on('electronStoreRemove', (ipcEvent, key) => {
        store.delete(key)
    })

}

//提供给渲染进程的日志接口
function loggerInit() {
    ipcMain.on('logger-info', (event, arg) => {
        logger.info(arg)
    })
    ipcMain.on('logger-warn', (event, arg) => {
        logger.warn(arg)
    })
    ipcMain.on('logger-error', (event, arg) => {
        logger.error(arg)
    })
}


function fileReadInit() {
    ipcMain.on('dialog:openFile', async (ipcEvent, key) => {
        console.log('dialog:openFile===============1')
        ipcEvent.returnValue = await getOpenFilePath()
    })

    //读取文件
    ipcMain.on('file:readFile', async (ipcEvent, filePath) => {
        ipcEvent.returnValue = await readFile(filePath)
    })
    //写入文件
    ipcMain.on('file:writeFile', async (ipcEvent, arg) => {
        const filePath  = store.get("FitTrackFilePath", null);
        ipcEvent.returnValue = await writeFile(filePath, arg)
    })
}

